<template>
  <div>
    <el-button type="primary" size="small" icon="plus" @click="handleAddParam()">添加</el-button>
    <el-alert
      style="line-height: 1.2; margin-bottom: 10px"
      title="1. color字段不会生效. 2. 系统会根据name排列顺序, 分别取名为keyword1.DATA,keyword2.DATA,..."
      type="info"
    />
    <el-table empty-text=" " :data="keywords" border size="small" style="width: 620px">
      <el-table-column prop="name" align="center" label="Name" />
      <el-table-column prop="value" align="left" label="Value">
        <template slot-scope="scope">
          <div style="white-space: pre-line;">{{ scope.row.value }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="color" align="center" label="Color" />
      <el-table-column prop="description" align="left" label="备注" />
      <el-table-column align="center" label="操作" width="160px">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="handleParamEdit(scope)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleParamDel(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-loading="loading" :show-close="false" title="模板参数" :visible.sync="dialogVisible" width="500px">
      <el-form ref="paramsForm" :rules="paramsRules" :model="paramsForm" label-position="left" label-width="60px">
        <el-form-item label="Name" prop="name">
          <el-input v-model="paramsForm.name" placeholder="参数名">
            <template slot="append">.DATA</template>
          </el-input>
        </el-form-item>

        <el-form-item label="Value" prop="value">
          <el-input v-model="paramsForm.value" type="textarea" placeholder="参数值" />
        </el-form-item>

        <el-form-item label="Color" prop="color">
          <el-color-picker v-model="paramsForm.color" />
        </el-form-item>

        <el-form-item label="备注" prop="description">
          <el-input v-model="paramsForm.description" type="textarea" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleParamCancel()">取 消</el-button>
        <el-button type="primary" @click="handleParamSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import MsgKeywordsDialog from './MsgKeywordsDialog'

export default {
  name: 'TemplateKeywordsConfig',
  components: {
    MsgKeywordsDialog
  },
  model: {
    prop: 'keywords',
    event: 'change'
  },
  props: {
    keywords: Array
  },
  data() {
    return {
      dialogVisible: false,
      paramsRules: {
        name: [{
          min: 1,
          max: 20,
          required: true,
          message: '请输入标题, 长度在 1 到 20 个字符',
          trigger: 'blur'
        }
        ],
        value: [{
          min: 1,
          max: 255,
          required: true,
          message: '请输入标题, 长度在 1 到 255 个字符',
          trigger: 'blur'
        }
        ]
      },
      loading: false,
      paramsForm: {
        $index: null,
        name: '',
        value: '',
        description: '',
        color: '#000000'
      }
    }
  },
  methods: {
    handleParamDel(scope) {
      this.keywords.splice(scope.$index, 1)
    },
    handleParamEdit(scope) {
      this.paramsForm = {
        $index: scope.$index,
        name: scope.row.name,
        value: scope.row.value,
        color: scope.row.color,
        description: scope.row.description
      }
      this.dialogVisible = true
    },
    handleParamCancel() {
      this.dialogVisible = false
    },
    handleParamSave() {
      this.loading = true
      this.$refs.paramsForm.validate(valid => {
        if (valid) {
          this.dialogVisible = false
          if (!this.paramsForm.$index && this.paramsForm.$index !== 0) {
            this.keywords.push({
              name: this.paramsForm.name,
              value: this.paramsForm.value,
              description: this.paramsForm.description,
              color: this.paramsForm.color
            })
          } else {
            this.$set(this.keywords, this.paramsForm.$index, this.paramsForm)
          }
        }
        this.loading = false
      })
    },
    handleAddParam() {
      this.paramsForm = {
        $index: null,
        name: '',
        value: '',
        description: '',
        color: '#000000'
      }
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped>

</style>
